//
//  ImageDataManager.m
//  SouDog
//
//  Created by  on 15/8/18.
//  Copyright (c) 2015年 @SHui. All rights reserved.
//

#import "ImageDataManager.h"

static ImageDataManager * g_manager =nil;

#define IMAGE_DIR_PATH [NSString stringWithFormat:@"%@/Documents/ImageDirect",NSHomeDirectory()]

@implementation ImageDataManager

+(id)sharedImageDataManager{

    static dispatch_once_t onceToken;
    dispatch_once(&onceToken,^{
    
        if(g_manager==nil){
            g_manager=[[ImageDataManager alloc]init];
        
        }
    
    });

    return g_manager;
}
-(instancetype)init{

    if (self=[super init]) {
        //如果文件夹不存在 就需要创建一个文件夹来存放
        NSFileManager * fileManager =[NSFileManager defaultManager];
        
        BOOL isExist =[fileManager fileExistsAtPath:IMAGE_DIR_PATH];
        if (isExist) {
            
        }else{
            //如果不存在就创建
            [fileManager createDirectoryAtPath:IMAGE_DIR_PATH withIntermediateDirectories:NO attributes:nil error:nil];
        
        }
    }
    return self;

}

-(BOOL)isImageDataExistByDBModel:(DBModel *)model{

    NSFileManager * fileManager =[NSFileManager defaultManager];
    //取出图片文件夹下边所有的数据的名称
    NSArray * arr =[fileManager contentsOfDirectoryAtPath:IMAGE_DIR_PATH error:nil];
     NSString * imageFileName =[NSString stringWithFormat:@"%@+%@",model.appId,model.name];
    if ([arr containsObject:imageFileName]) {
        return YES;
    }else{
        return NO;
    }

}

-(BOOL)addImageDataWithDBModel:(DBModel *)model{
    BOOL isExist =[self isImageDataExistByDBModel:model];
    if (isExist) {
        NSLog(@"图片已经存在");
        return NO;
    }else{
       
        NSString * imageFileName =[NSString stringWithFormat:@"%@+%@",model.appId,model.name];

    // 将图片转换成二进制的数据 存入沙盒
        
        NSString *imageFilePath = [NSString stringWithFormat:@"%@/%@",IMAGE_DIR_PATH,imageFileName];
        
        // 取出图片二进制数据
        NSData *imageData = [NSData dataWithContentsOfFile:imageFilePath];
        
        BOOL saveSuc =[imageData writeToFile:imageFilePath atomically:YES];
        
        if (saveSuc) {
            return YES;
        }else{
            return NO;
        }
    }

}
-(BOOL)deleteImageDataWithDBmodel:(DBModel *)model{

    BOOL isExist = [self isImageDataExistByDBModel:model];
    if (isExist) {
        NSFileManager *manager = [NSFileManager defaultManager];
        // 得到存储图片的路径
        // 1.获取图片名
        NSString * imageFileName =[NSString stringWithFormat:@"%@+%@",model.appId,model.name];
       
        NSString *imageFilePath = [NSString stringWithFormat:@"%@/%@",IMAGE_DIR_PATH,imageFileName];
        
        BOOL isDeleteSuc =[manager removeItemAtPath:imageFilePath error:nil];
        if (isDeleteSuc) {
            
            NSLog(@"删除图片成功");
            return YES;
        }else {
            NSLog(@"删除图片失败");
            return NO;
        }
    }else {
        NSLog(@"该图片数据不存在");
        return NO;
    }

}
-(NSDictionary *)fetchImageWithDBModel:(DBModel *)model{

    // 得到存储图片的路径
    // 1.获取图片名
    NSString *imageFileName = [NSString stringWithFormat:@"%@+%@",model.appId,model.name];
    // 2.获取图片路径
    NSString *imageFilePath = [NSString stringWithFormat:@"%@/%@",IMAGE_DIR_PATH,imageFileName];
    
    // 取出图片二进制数据
    NSData *imageData = [NSData dataWithContentsOfFile:imageFilePath];
    
    NSDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:imageData options:NSJSONReadingMutableLeaves error:nil];
  
    return jsonDict;

}
@end
